import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import VueDevTools from 'vite-plugin-vue-devtools'

import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import { PrimeVueResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'

import UnoCSS from 'unocss/vite'

const publicDir = resolve('resources')
const envDir = resolve('build')

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    const baseUrl = loadEnv(mode, process.cwd()).VITE_BASE_URL

    return {
        base: baseUrl,
        plugins: [
            vue(),
            vueJsx(),
            VueDevTools(),
            AutoImport({
                resolvers: [
                    PrimeVueResolver({
                        // importTheme: 'saga-blue'
                    })
                ],
                imports: ['vue', 'vue-router']
            }),
            Components({
                resolvers: [
                    PrimeVueResolver({
                        // importTheme: 'saga-blue'
                    })
                ]
            }),
            VueSetupExtend(),
            UnoCSS()
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            }
        },
        css: {
            preprocessorOptions: {
                less: {
                    charset: false
                }
            }
        },
        server: {
            host: '0.0.0.0', // 默认是 localhost
            port: 5010, // 默认是 3000 端口
            open: true, // 浏览器自动打开
            proxy: {
                // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
                // '/remote-beta': {
                //     target: 'http://192.168.0.39:9094', // 后端服务实际地址
                //     changeOrigin: true,
                //     rewrite: (path) => path.replace(/^\/remote-beta/, '')
                // }
                // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
                '/remote-beta': {
                    target: 'https://cgs-beta.ourwill.cn', // 后端服务实际地址
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/remote-beta/, '')
                }
            }
        }
    }
})
